package demo;

/**
 * Created by qiang.yu on 2017/4/27.
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
 *
 *@author huyongjian Oracle(Compus Solution Group)
 * @Date  2013-7-18
 * @version 2.0
 * @since JDK1.6（建议）
Copy Right Information    COMPUS SOLUTION GROUP
IDE:Eclipse
class:Ball 双色球
 */
public class Ball extends JFrame implements ActionListener{
    JPanel pn1,pn2,pn3;//窗体上有三个面板
    JTextField tf1,tf2;//两个文本框
    JLabel lb1,lb2,lb3;//
    JButton btn1,btn2,btn3;//三个按钮
    boolean flag = false;//定义一个标记
    Ball(){
        pn1 = new JPanel();
        pn2 = new JPanel();
        pn3 = new JPanel();
        tf1 = new JTextField("红色球",20);//长度20
        tf2 = new JTextField("蓝色球",8);//长度8
        btn1 = new JButton("开始");
        btn2 = new JButton("停止");
        btn3 = new JButton("关于");
        this.setLayout(new BorderLayout());
        this.add(pn1,BorderLayout.NORTH);//窗体边框布局，默认情况下是CENTER
        this.add(pn2,BorderLayout.CENTER);
        this.add(pn3,BorderLayout.SOUTH);
        lb2 = new JLabel("红色球从1到33中选择六个球");
        lb2.setForeground(Color.black);
        lb3 = new JLabel("蓝色球从1到16中选择一个球");
        lb3.setForeground(Color.black);
//      FlowLayout flow = new FlowLayout();
//      pn1.setLayout(flow);
        Font font1=new Font("黑体",Font.BOLD,12);
        pn1.add(lb2);
        pn1.add(tf1);
        tf1.setForeground(Color.red);
        tf1.setFont(font1);
        pn1.add(lb3);
        pn1.add(tf2);
        tf2.setForeground(Color.blue);
        tf2.setFont(font1);
        Font fnt = new Font("华文隶书",Font.BOLD,20);
        lb1 = new JLabel("双色球奖池已达到458398235元，祝你好运！！！");
        lb1.setFont(fnt);
        lb1.setForeground(Color.red);
        pn2.add(lb1);
        pn3.add(btn1);
        pn3.add(btn2);
        pn3.add(btn3);
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        this.setSize(600,150);//窗体大小
        //显示位置居中
        this.setLocationRelativeTo(null);//显示位置居中
        //修改java左上角的图标
        URL url = this.getClass().getResource("ball.png");
        Image img = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(img);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//程序关闭时结束javaw.exe的运行
        this.setTitle("双色球自动抽奖机");
        this.setVisible(true);
        this.setResizable(false);
        start();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == btn1){
            btn1.setEnabled(false);
            btn2.setEnabled(true);
            flag = true;
        }

        if(e.getSource() == btn2){
            btn2.setEnabled(false);
            btn1.setEnabled(true);
            flag = false;
        }
        if(e.getSource()==btn3){
            JOptionPane.showMessageDialog(null, "<html><body>程序参与人员 :<br>技 术 总 监&nbsp; : &nbsp;&nbsp;杨强<br>分 析 设 计&nbsp; :&nbsp;&nbsp;小夜的传说<br>代 码 编 写&nbsp; :&nbsp;&nbsp;小夜的传说<br>E-mail&nbsp; :&nbsp;&nbsp;yongjian.hu@oraclecsg.com<body></html>");

        }


    }
    public void start(){
        String str1 = "";
        String str2 = "";
        while(true){
            if(flag){
                str1 = "";
                for(int i = 0; i < 6; i++){
                    int m = (int)(Math.random() * 32 + 1);
                    if (m < 10){
                        str1 = str1 + " 0" + m;
                    }
                    else{
                        str1 = str1 + " " + m;
                    }
                }
                str2 = "";
                for(int i=0;i<2;i++){
                    int n = (int)(Math.random() * 16 + 1);


                    if(n<10){
                        str2=str2+" 0"+n;
                    }
                    else{
                        str2=str2+" "+n;
                    }
                }
            }
            try{
                Thread.sleep(10);
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
            tf1.setText(str1);
            tf2.setText(str2);
        }
    }
}
